﻿using CYQ.Data;
using CYQ.Data.Table;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBManager
{
    /// <summary>
    /// 1：MySQL 5.7.9版本需要把用命令行设置： 执行SET GLOBAL sql_mode = ''; 把sql_mode 改成非only_full_group_by模式。验证是否生效 SELECT @@GLOBAL.sql_mode 或 SELECT @@sql_mode
    /// 优酷电影条目数据库操作类
    /// </summary>
    public class YouKuTable
    {
        static string tableName = "yk_film";
        public static bool InsertItem(YouKuFilmItem _item)
        {
            bool rs = false;
            using (MAction action = new MAction(tableName))
            {
                action.Set("yk_film_id", _item.VideoId);
                action.Set("yk_film_name", _item.VideoName);
                action.Set("yk_film_url", _item.VideoURL);
                action.Set("yk_film_img", _item.VideoImgURL);
                rs = action.Insert();
            }
            return rs;
        }

        public static bool InsertItems(List<YouKuFilmItem> _items)
        {
            bool rs = false;
            using (MAction action = new MAction(tableName))
            {
                action.BeginTransation();
                foreach (var _item in _items)
                {
                    action.Set("yk_film_id", _item.VideoId);
                    action.Set("yk_film_name", _item.VideoName);
                    action.Set("yk_film_url", _item.VideoURL);
                    action.Set("yk_film_img", _item.VideoImgURL);
                    rs = action.Insert();
                }
                action.EndTransation();
            }
            return rs;
        }

        public static bool hasThis(YouKuFilmItem _item)
        {
            bool rs = false;
            using (MAction action = new MAction(tableName))
            {
                string whereStr = "yk_film_id='{0}'";
                whereStr = string.Format(whereStr, _item.VideoId);
                MDataTable dt = action.Select(whereStr);
                rs = dt.Rows.Count > 0;
            }
            return rs;
        }

        public static List<string> GetAllFilmInTable()
        {
            using (MAction action = new MAction(tableName))
            {
                MDataTable dt = action.Select();
                return dt.GetColumnItems<string>("yk_film_img");
            }
        }
    }
}
